﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace FindPath
{
    public partial class SplashForm : Form
    {
        public SplashForm()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
        }

        private bool loadWarpFile()
        {
            if (File.Exists(Constants.WARP_CONFIG))
            {
                updateLoading("Loading warp data...");
                string[] fileLines = File.ReadAllLines(Constants.WARP_CONFIG);
                foreach (string eachLine in fileLines)
                {
                    string[] lineEles = eachLine.Split('\t');
                    if (lineEles[0] == "1")
                    {
                        int mapId = int.Parse(lineEles[1]);
                        int warpId = int.Parse(lineEles[2]);
                        int childMapId = int.Parse(lineEles[3]);
                        Place parentMap = null;

                        if (!Global.globalPlaces.ContainsKey(mapId))
                        {
                            parentMap = new Place { mapID = mapId, name = "", parent = new KeyValuePair<int, Place>(-1, null), child = new Dictionary<int, Place>() };
                            Global.globalPlaces.Add(mapId, parentMap);
                        }
                        else
                        {
                            parentMap = Global.globalPlaces[mapId];
                        }
                        Place newChild = null;
                        if (!Global.globalPlaces.ContainsKey(childMapId))
                        {
                            newChild = new Place { mapID = childMapId, name = "", parent = new KeyValuePair<int, Place>(-1, null), child = new Dictionary<int, Place>() };
                            Global.globalPlaces.Add(childMapId, newChild);
                        }
                        else
                        {
                            newChild = Global.globalPlaces[childMapId];
                        }
                        if (!parentMap.child.ContainsKey(warpId))
                        {
                            parentMap.child.Add(warpId, newChild);
                        }

                    }
                    else if (lineEles[0] == "2")
                    {
                        int mapId = int.Parse(lineEles[1]);
                        String mapName = lineEles[2];
                        if (Global.globalPlaces.ContainsKey(mapId))
                        {
                            Place place = Global.globalPlaces[mapId];
                            place.name = mapName;
                        }
                    }
                }
                //buildWarpTree();
                return true;
            }
            else
            {
                Debug.WriteLine("FILE WARP NOT FOUND");
                updateLoading("FILE WARPID.ini NOT FOUND");
                return false;
            }
        }

        private bool loadNpcFile()
        {
            if (File.Exists(Constants.NPC_CONFIG))
            {
                updateLoading("Loading npc data...");
                string[] fileLines = File.ReadAllLines(Constants.NPC_CONFIG);
                foreach (string eachLine in fileLines)
                {
                    string[] lineEles = eachLine.Split('\t');
                    int npcId = int.Parse(lineEles[0]);
                    string npcName = lineEles[1];
                    TsNpc newNPC = new TsNpc { npcId = npcId, name = npcName };
                    Global.globalNpc.Add(npcId, newNPC);
                }
                return true;
            }
            else
            {
                Debug.WriteLine("FILE NPC NOT FOUND");
                updateLoading("FILE NPC.ini NOT FOUND");
                return false;
            }
        }

        private bool loadSkillFile()
        {
            if (File.Exists(Constants.SKILL_CONFIG))
            {
                updateLoading("Loading skill data...");
                string[] fileLines = File.ReadAllLines(Constants.SKILL_CONFIG);
                foreach (string eachLine in fileLines)
                {
                    string[] lineEles = eachLine.Split('\t');
                    int skillId = int.Parse(lineEles[0]);
                    string skillName = lineEles[1];
                    int skillSp = int.Parse(lineEles[2]);
                    TsSkill newSkill = new TsSkill { skillId = skillId, name = skillName };
                    Global.globalSkills.Add(skillId, newSkill);
                }
                return true;
            }
            else
            {
                Debug.WriteLine("FILE SKILL NOT FOUND");
                updateLoading("FILE skill.ini NOT FOUND");
                return false;
            }
        }
        private bool loadItemData()
        {
            if (File.Exists(Constants.ITEM_CONFIG))
            {
                updateLoading("Loading item data...");
                string[] fileLines = File.ReadAllLines(Constants.ITEM_CONFIG);
                foreach (string eachLine in fileLines)
                {
                    string[] lineEles = eachLine.Split('\t');
                    int itemId = int.Parse(lineEles[0]);
                    string itemName = lineEles[1];
                    string itemDesc = "";
                    if (lineEles.Length > 2)
                    {
                        itemDesc = lineEles[2];
                    }
                    TsItem newItem = new TsItem { itemId = itemId, name = itemName, description = itemDesc};
                    Global.globalItems.Add(itemId, newItem);
                }
                return true;
            }
            else
            {
                Debug.WriteLine("FILE ITEM NOT FOUND");
                updateLoading("FILE item.ini NOT FOUND");
                return false;
            }
        }
        private void calculateExpChart()
        {
            for (int i = 1; i <= 200; i++)
            {
                Global.expChart[0, i] = (int) (Global.expChart[0, i - 1] + Math.Floor(Math.Pow(i + 1, 2.9)) + 5);
                Global.expChart[1, i] = (int)(Global.expChart[1, i - 1] + Math.Floor(Math.Pow(i + 1, 3)) + 5);
                Global.expChart[2, i] = (int)(Global.expChart[2, i - 1] + Math.Floor(Math.Pow(i + 1, 3.1)) + 5);
            }
        }
        private void updateLoading(string announce)
        {
            lblProgressStt.Text = announce;
            pgrLoadingProgress.Step++;
            pgrLoadingProgress.PerformStep();
        }
        private void lblProgressStt_Click(object sender, EventArgs e)
        {

        }

        private void SplashForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (loadNpcFile() && loadItemData() && loadSkillFile() && loadWarpFile())
                {
                    calculateExpChart();
                    this.Close();
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                this.Close();
            }
        }
    }
}
